<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item :to="{ path: '/' }">教师</el-breadcrumb-item>
      <el-breadcrumb-item>课程设计任务</el-breadcrumb-item>
      <el-breadcrumb-item>管理课程设计任务</el-breadcrumb-item>
    </el-breadcrumb>

    <div>
      <h2 style="border-bottom: 1px solid;padding-bottom: 10px;">管理课程设计任务</h2>
    </div>

    <div style="margin: 3% 5% 3% 5%;">
      <!-- 下拉框 -->
      <el-row :gutter="20" style=" margin-top: 20px; ">
        <el-col :span="6">
          <div class="grid-content ep-bg-purple">
            <el-button type="primary" :icon="Plus" @click="toTaskBase">发布</el-button>
          </div>
        </el-col>
      </el-row>

      <!-- 表格1 -->
      <h1>我的发布</h1>
      <el-table :data="myPublish" height="550" style="width: 100%; margin-top: 1%;">
        <el-table-column prop="courseName" label="实践课程" />
        <el-table-column prop="majorName" label="专业" />
        <el-table-column prop="className" label="班级" />
        <el-table-column prop="onloadDate" label="时间" :formatter="formatDate" />
        <el-table-column fixed="right" label="任务详情" width="120">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="toPost(scope.row.practicalId)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

  </div>
</template>
<script setup>
import { ArrowRight, Calendar, Search, Plus } from '@element-plus/icons-vue'
import { ref, onBeforeMount } from 'vue'
import { useRouter } from 'vue-router'
import requests from '@/api/ajax.js'
const router_useRouter = useRouter()
function toPost(practicalId) {

  const asd = requests({ url: "/inner/taskbaseVo/" + practicalId, method: "get", }).then(res => {

    if (Object.keys(res.rdata).length === 0) {
      ElMessage({
        message: '还未发布课程设计任务，请现发布任务',
        type: 'error',
      })
    } else {

      router_useRouter.push({
        path: 'teaTaskDetail',
        query: { practicalId: practicalId }
      })
    }

  })
    .catch(err => {
      console.error(err);
      // 登录请求失败后的操作
    });



  // $router.push(`courseTaskDetail/${scope.row.practicalId}`)
}


onBeforeMount(() => {
  //初始显示老师所有的发布题目
  const asd = requests({ url: "/inner/MycoursePublish", method: "get", }).then(res => {
    //存储用户信息到浏览器toke

    const abc = res.rdata
    myPublish.value = abc
  })
    .catch(err => {
      console.error(err);
      // 登录请求失败后的操作
    });

  //   //请求获取老师可选择的班级
  // const acd = requests({ url: "/course/course", method: "get", }).then(res => {
  //   //存储用户信息到浏览器toke
  //   const abc = res.rdata
  //   courseOptions.value = abc

  // })
  //   .catch(err => {
  //     console.error(err.data);
  //     // 登录请求失败后的操作
  //   });

  // //获取老师可选择的班级和课程编号
  // const aad = requests({ url: "/course/class", method: "get", }).then(res => {
  //   //存储用户信息到浏览器toke
  //   const abc = res.rdata
  //   classNum.value = abc
  //   // console.log(classNum.value)

  // })
  //   .catch(err => {
  //     console.error(err.data);
  //     // 登录请求失败后的操作
  //   });
  // console.log(myPublish.value)
})

//接收课题发布的学期
const publishtask = ref([

])

// 题目种类
const classNum = ref([

])

const myPublish = ref([

])

// 课程类型
const courseOptions = ref([
  {
    courseId: '',
    courseName: '',
  },
])

//列表信息
const xmyPublish = ref([
  {
    publishId: '',
    memberMax: '',
    taskName: "",
    content: "",
    scaleRule: "",
    startDate: "",
    endDate: "",
    planId: '',
    courseId: '',
    courseName: "",
    majorName: "",
    enrolDate: "",
    practicalId: '',
    classId: '',
    className: "",
    adviserId: '',
    adviserName: "",
    teachId: '',
    teachName: "",
    practicalType: '',
    onloadDate: ""
  },
])

//时间格式化
function formatDate(row, column, cellValue, index) {
  let format = 'YYYY-mm'
  let date = new Date(cellValue);
  const dataItem = {
    'Y+': date.getFullYear().toString(),
    'm+': (date.getMonth() + 1).toString(),
    'd+': date.getDate().toString(),
  }
  Object.keys(dataItem).forEach((item) => {
    const ret = new RegExp(`(${item})`).exec(format);
    if (ret) {
      format = format.replace(ret[1], ret[1].length === 1 ? dataItem[item] : dataItem[item].padStart(ret[1].length, '0'));
    }
  });
  return format
}

function changeDate() {
  for (let i = 0; i < myPublish.value.length; i++) {
    const obj = {}//每次循环创建一个新的对象
    obj.courseDate = myPublish.value[i].courseDate
    publishtask.value.push(obj);
    publishtask.value[i].courseValue = myPublish.value[i].courseDate
  }

}


function toTaskBase() {
  router_useRouter.push({ path: 'courseBase' })
}

</script>